using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
namespace HKB.Common
{
	public static class StaticFun
	{
		private static System.Random _rd = null;
		private static string imgNoPath = "Skin/CommStyle/images/noneimg.jpg";
		public static bool IsNeedCheckIdentifyRegCode
		{
			get
			{
				return true;
			}
		}
		public static bool IsShowAdminLeftMunuRightInfo
		{
			get
			{
				return true;
			}
		}
		public static System.Random RD
		{
			get
			{
				if (StaticFun._rd == null)
				{
					StaticFun._rd = new System.Random();
				}
				return StaticFun._rd;
			}
		}
		public static string ToString2(this object obj)
		{
			string result;
			try
			{
				result = System.Convert.ToDouble(obj).ToString("F2");
			}
			catch
			{
				result = "0.00";
			}
			return result;
		}
		public static decimal ToDecimal2(this object obj)
		{
			decimal result;
			try
			{
				result = decimal.Round(System.Convert.ToDecimal(obj), 2);
			}
			catch
			{
				result = 0.00m;
			}
			return result;
		}
		public static int ToInt32(this object obj, params int[] defaultValue)
		{
			int result;
			try
			{
				result = System.Convert.ToInt32(obj);
			}
			catch
			{
				if (defaultValue.Length == 0)
				{
					result = 0;
				}
				else
				{
					result = defaultValue[0];
				}
			}
			return result;
		}
		public static long ToInt64(this object obj, params long[] defaultValue)
		{
			long result;
			try
			{
				result = System.Convert.ToInt64(obj);
			}
			catch
			{
				if (defaultValue.Length == 0)
				{
					result = 0L;
				}
				else
				{
					result = defaultValue[0];
				}
			}
			return result;
		}
		public static System.DateTime ToDateTime(this object obj, params System.DateTime[] defaultValue)
		{
			System.DateTime result;
			try
			{
				result = System.Convert.ToDateTime(obj);
			}
			catch
			{
				if (defaultValue.Length == 0)
				{
					result = System.DateTime.MinValue;
				}
				else
				{
					result = defaultValue[0];
				}
			}
			return result;
		}
		public static string RandCode(int n, int number)
		{
			char[] array = new char[]
			{
				'a',
				'b',
				'd',
				'c',
				'e',
				'f',
				'g',
				'h',
				'i',
				'j',
				'k',
				'l',
				'm',
				'n',
				'p',
				'r',
				'q',
				's',
				't',
				'u',
				'v',
				'w',
				'z',
				'y',
				'x',
				'A',
				'B',
				'C',
				'D',
				'E',
				'F',
				'G',
				'H',
				'I',
				'J',
				'K',
				'L',
				'M',
				'N',
				'Q',
				'P',
				'R',
				'T',
				'S',
				'V',
				'U',
				'W',
				'X',
				'Y',
				'Z'
			};
			System.Random random = new System.Random(System.DateTime.Now.Millisecond);
			string text = "";
			for (int i = 0; i < number; i++)
			{
				System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
				for (int j = 0; j < n; j++)
				{
					stringBuilder.Append(array[random.Next(0, array.Length)].ToString());
				}
				text = text + stringBuilder.ToString() + ",";
			}
			if (text != "")
			{
				text = text.Substring(0, text.Length - 1);
			}
			return text;
		}
		public static string TimeString(this System.DateTime dt)
		{
			return dt.ToString("yyyyMMddHHmmssfff") + StaticFun.RD.Next(100, 999);
		}
		public static string GetDataRow(this DataRow dr, string ColumnName)
		{
			string result;
			if (dr[ColumnName] == null || dr[ColumnName] == System.DBNull.Value)
			{
				result = "";
			}
			else
			{
				result = dr[ColumnName].IsNull(new object[0]);
			}
			return result;
		}
		public static string GetRBLValue(this RadioButtonList rbl)
		{
			string result;
			if (rbl == null || rbl.SelectedIndex < 0 || rbl.Items.Count == 0)
			{
				result = string.Empty;
			}
			else
			{
				result = rbl.SelectedValue;
			}
			return result;
		}
		public static string GetUserNameByid(int? userid)
		{
			string result;
			if (!userid.HasValue || userid <= 0)
			{
				result = "";
			}
			else
			{
				object single = Conn.GetSingle("select UserName from HL_User where UserID=" + userid, new IDbDataParameter[0]);
				result = single.IsNull(new object[0]);
			}
			return result;
		}
		public static int GetUserIDByName(string userName)
		{
			int result;
			if (userName.Trim() == "")
			{
				result = 0;
			}
			else
			{
				object single = Conn.GetSingle("select top 1 UserID from HL_User where UserName=" + userName, new IDbDataParameter[0]);
				result = single.IsNull(new object[0]).ToInt32(new int[0]);
			}
			return result;
		}
		public static string IsNull(this object obj, params object[] defaultValue)
		{
			string result;
			if (obj == System.DBNull.Value || obj == null || "".Equals(obj.ToString().Trim()))
			{
				if (defaultValue.Length == 0 || defaultValue[0] == null)
				{
					result = "";
				}
				else
				{
					result = defaultValue[0].ToString();
				}
			}
			else
			{
				result = obj.ToString();
			}
			return result;
		}
		public static string DataTableToJson(this DataTable dt)
		{
			string text = string.Empty;
			string result;
			try
			{
				if (dt.Rows.Count == 0)
				{
					result = "({})";
					return result;
				}
				text = "({";
				object obj = text;
				text = string.Concat(new object[]
				{
					obj,
					"'table",
					1,
					"':["
				});
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					text += "{";
					for (int j = 0; j < dt.Columns.Count; j++)
					{
						string text2 = text;
						text = string.Concat(new string[]
						{
							text2,
							"'",
							dt.Columns[j].ColumnName,
							"':'",
							dt.Rows[i][j].ToString(),
							"'"
						});
						if (j != dt.Columns.Count - 1)
						{
							text += ",";
						}
					}
					text += "}";
					if (i != dt.Rows.Count - 1)
					{
						text += ",";
					}
				}
				text += "]";
				text += "})";
			}
			catch (System.Exception ex)
			{
				throw new System.Exception(ex.Message);
			}
			result = text;
			return result;
		}
		public static void WriteErrorLog(this System.Exception ex, params string[] remark)
		{
			string path = HttpContext.Current.Server.MapPath(string.Format("/App_Data/ErrorLog{0}.txt", System.DateTime.Today.ToString("yyyy_MM_dd")));
			using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, true, System.Text.Encoding.Default))
			{
				streamWriter.WriteLine(string.Concat(new string[]
				{
					"*** Error Time： ",
					System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff"),
					" ***[",
					(remark.Length > 0) ? remark[0] : "",
					"]"
				}));
				if (HttpContext.Current.Request.UrlReferrer != null)
				{
					streamWriter.WriteLine("*** Refer Url： " + HttpContext.Current.Request.UrlReferrer.AbsoluteUri.IsNull(new object[0]) + " ***");
				}
				streamWriter.WriteLine("*** Error Page： " + HttpContext.Current.Request.Url.AbsoluteUri + " ***");
				streamWriter.WriteLine(ex.ToString() + System.Environment.NewLine);
				streamWriter.Close();
			}
		}
		public static string GetShortString(this object obj, int n, params string[] EndString)
		{
			string text = (obj != null) ? obj.ToString() : "";
			string text2 = string.Empty;
			string result;
			if (System.Text.Encoding.Default.GetByteCount(text) <= n)
			{
				result = text;
			}
			else
			{
				int num = 0;
				char[] array = text.ToCharArray();
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i] >= '一' && array[i] <= '龥')
					{
						text2 += array[i];
						num += 2;
					}
					else
					{
						text2 += array[i];
						num++;
					}
					if (num >= n)
					{
						break;
					}
				}
				text2 += "…";
				result = text2;
			}
			return result;
		}
		public static DataTable SubTable(this DataTable dtold, int n, int PageIndex)
		{
			if (PageIndex < 1)
			{
				PageIndex = 1;
			}
			int num = n * (PageIndex - 1);
			DataTable result;
			if (dtold == null || dtold.Rows.Count == 0)
			{
				result = dtold;
			}
			else
			{
				if (dtold.Rows.Count <= num)
				{
					for (int i = 0; i < dtold.Rows.Count; i++)
					{
						dtold.Rows[i].Delete();
					}
					dtold.AcceptChanges();
					result = dtold;
				}
				else
				{
					int num2 = dtold.Rows.Count - n * PageIndex;
					int num3 = n * (PageIndex - 1);
					for (int i = 1; i <= num; i++)
					{
						dtold.Rows[i - 1].Delete();
					}
					for (int i = 1; i <= num2; i++)
					{
						dtold.Rows[dtold.Rows.Count - i].Delete();
					}
					dtold.AcceptChanges();
					result = dtold;
				}
			}
			return result;
		}
		public static DataTable PlusTable(this DataTable dtold, int n, params bool[] hasRowid)
		{
			DataTable result;
			if (dtold == null || dtold.Rows.Count == 0 || dtold.Rows.Count >= n)
			{
				result = dtold;
			}
			else
			{
				int num = n - dtold.Rows.Count;
				bool flag = hasRowid.Length > 0 && hasRowid[0];
				for (int i = 1; i <= num; i++)
				{
					DataRow dataRow = dtold.NewRow();
					dataRow.ItemArray = dtold.Rows[dtold.Rows.Count - 1].ItemArray;
					if (flag)
					{
						dataRow["rowid"] = n - 1 - num + i;
					}
					dtold.Rows.Add(dataRow.ItemArray);
				}
				dtold.AcceptChanges();
				result = dtold;
			}
			return result;
		}
		public static void StringEncrypt(string[] strs)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			for (int i = 0; i < strs.Length; i++)
			{
				stringBuilder.AppendFormat("{0} -> {1}<br/>", strs[i], strs[i].Encrypt());
			}
			HttpContext.Current.Response.Write(stringBuilder.ToString());
		}
		public static string GetPruductImg(this object obj)
		{
			string text = obj.IsNull(new object[0]);
			string result;
			if (text != "")
			{
				if (text.StartsWith("."))
				{
					text = "/store" + text.TrimStart(new char[]
					{
						'.'
					});
				}
				if (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(text)))
				{
					result = StaticFun.imgNoPath;
				}
				else
				{
					result = text;
				}
			}
			else
			{
				result = StaticFun.imgNoPath;
			}
			return result;
		}
		public static void DeleteUnUseProductPic(string ParentDir)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			string commandText = "delete from HLShop_ProductImg where ProductID not in(select ProductID from HLShop_Product)";
			Conn.ExecuteSql(commandText, new IDbDataParameter[0]);
			DataTable dataTable = Conn.Query(" SELECT productimageurl,productsmallimageurl FROM HLShop_Product hp", new IDbDataParameter[0]).Tables[0];
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				if (dataTable.Rows[i]["productimageurl"] != System.DBNull.Value)
				{
					string text = dataTable.Rows[i]["productimageurl"].IsNull(new object[0]);
					if (text != "")
					{
						if (!arrayList.Contains(text))
						{
							arrayList.Add(text);
						}
						if (text.Contains("_small"))
						{
							text = text.Replace("_small", "");
							if (!arrayList.Contains(text))
							{
								arrayList.Add(text);
							}
						}
					}
				}
				if (dataTable.Rows[i]["productsmallimageurl"] != System.DBNull.Value)
				{
					string text2 = dataTable.Rows[i]["productsmallimageurl"].IsNull(new object[0]);
					if (text2 != "")
					{
						if (!arrayList.Contains(text2))
						{
							arrayList.Add(text2);
						}
					}
				}
			}
			dataTable = Conn.Query(" SELECT ImgPath1,ImgPath2,ImgPath3,ImgPath4,ImgPath5 FROM HLShop_Product hp", new IDbDataParameter[0]).Tables[0];
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				int count = dataTable.Columns.Count;
				for (int j = 1; j <= count; j++)
				{
					string text = dataTable.Rows[i]["ImgPath" + j].IsNull(new object[0]);
					if (text != "" && !arrayList.Contains(text))
					{
						arrayList.Add(text);
					}
				}
			}
			dataTable = Conn.Query("select sp_value_image from [HLshop_spec_value] where isnull(sp_value_image,'')<>''", new IDbDataParameter[0]).Tables[0];
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				string text = dataTable.Rows[i]["sp_value_image"].IsNull(new object[0]);
				if (text != "" && !arrayList.Contains(text))
				{
					arrayList.Add(text);
				}
			}
			StaticFun.DeleteUnUseProductPic2(arrayList, ParentDir, "*.jpg");
			StaticFun.DeleteUnUseProductPic2(arrayList, ParentDir, "*.bmp");
			StaticFun.DeleteUnUseProductPic2(arrayList, ParentDir, "*.png");
		}
		private static void DeleteUnUseProductPic2(System.Collections.ArrayList list, string ParentDir, string SearchPattern)
		{
			string path = HttpContext.Current.Server.MapPath(ParentDir);
			string[] files = System.IO.Directory.GetFiles(path, SearchPattern, System.IO.SearchOption.TopDirectoryOnly);
			string[] array = files;
			for (int i = 0; i < array.Length; i++)
			{
				string path2 = array[i];
				string fileName = System.IO.Path.GetFileName(path2);
				string item = (ParentDir + "/" + fileName).Replace("//", "/").ToLower();
				if (!list.Contains(item))
				{
					if (System.IO.File.Exists(path2))
					{
						System.IO.File.Delete(path2);
					}
				}
			}
			string[] directories = System.IO.Directory.GetDirectories(path);
			string oldValue = HttpContext.Current.Server.MapPath("/");
			array = directories;
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				string parentDir = text.Replace(oldValue, "").Replace("\\", "/");
				StaticFun.DeleteUnUseProductPic2(list, parentDir, SearchPattern);
			}
		}
		public static int UpdateProductImage()
		{
			string commandText = "UPDATE HLShop_Product SET productimageurl =productsmallimageurl where productimageurl='' or productimageurl is null";
			return Conn.ExecuteSql(commandText, new IDbDataParameter[0]);
		}
		public static string GetError(this System.Exception er)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			try
			{
				er.WriteErrorLog(new string[]
				{
					"页面崩溃"
				});
				string text = er.ToString();
				int num = text.IndexOf(':');
				int num2 = text.LastIndexOf('行');
				if (num2 != -1)
				{
					int num3 = text.LastIndexOf('在', num2);
				}
				if (stringBuilder.Length == 0)
				{
					bool flag = false;
					if (text.Contains("输入字符串的格式不正确"))
					{
						flag = true;
						stringBuilder.Append("输入字符串的格式不正确!<br/>");
					}
					else
					{
						if (text.Contains("尝试除以零"))
						{
							flag = true;
							stringBuilder.Append("尝试除以零!<br/>");
						}
						else
						{
							if (text.Contains("索引超出了数组界限"))
							{
								flag = true;
								stringBuilder.Append("索引超出了数组界限!<br/>");
							}
							else
							{
								if (text.Contains("未将对象引用设置到对象的实例"))
								{
									flag = true;
									stringBuilder.Append("对象不存在或对象的值为空!<br/>");
								}
								else
								{
									if (text.Contains("CompileException"))
									{
										flag = true;
										stringBuilder.Append("编译错误!<br/>");
										stringBuilder.AppendFormat("位置：{0}<br/>", er.Message.Substring(er.Message.LastIndexOf('\\') + 1));
									}
									else
									{
										if (text.Contains("未找到或无法访问服务器"))
										{
											flag = true;
											stringBuilder.Append("无法连接到数据库服务器<br/>");
										}
									}
								}
							}
						}
					}
					if (flag)
					{
						num = text.IndexOf('位');
						num2 = text.IndexOf('行', num);
						int num3 = text.IndexOf('在', num2);
						string text2 = text.Substring(num, num2 - num - 1);
						stringBuilder.AppendFormat("位置：{0}<br/>{1}<br/>", text2.Substring(text2.LastIndexOf('\\')), text.Substring(num2, num3 - num2 - 1));
					}
				}
			}
			catch
			{
				stringBuilder.Append("未知错误！");
			}
			if (stringBuilder.Length == 0)
			{
				stringBuilder.Append(er.Message);
			}
			stringBuilder.Append("<br/><br/><font color='gray'><i>提示：管理员可查看日志文件跟踪错误</i></font>");
			return stringBuilder.ToString();
		}
		public static string GetContentByName(string TitleName, string ContentStr)
		{
			string result;
			if (!string.IsNullOrEmpty(ContentStr))
			{
				string[] array = ContentStr.Split(new char[]
				{
					'#'
				});
				if (array.Length > 1)
				{
					if (array[0] == TitleName)
					{
						result = array[1];
						return result;
					}
				}
			}
			result = "";
			return result;
		}
		public static string GetContentByName(string TitleName, string[] ContentArray)
		{
			string result;
			if (ContentArray != null && ContentArray.Length > 0)
			{
				for (int i = 0; i < ContentArray.Length; i++)
				{
					string[] array = ContentArray[i].Split(new char[]
					{
						'#'
					});
					if (array.Length > 1)
					{
						if (array[0] == TitleName)
						{
							result = array[1];
							return result;
						}
					}
				}
			}
			result = "";
			return result;
		}
		public static int GetValueInt32(DataTable dt, int dt_row_index, string ColumnsName)
		{
			int result = 0;
			try
			{
				if (dt.Columns.Contains(ColumnsName))
				{
					if (dt.Rows[dt_row_index][ColumnsName] != System.DBNull.Value)
					{
						string value = dt.Rows[dt_row_index][ColumnsName].ToString();
						if (!string.IsNullOrEmpty(value))
						{
							result = System.Convert.ToInt32(value);
						}
					}
				}
			}
			catch
			{
				result = 0;
			}
			return result;
		}
		public static decimal GetValueDecimal(DataTable dt, int dt_row_index, string ColumnsName)
		{
			decimal result = 0m;
			try
			{
				if (dt.Columns.Contains(ColumnsName))
				{
					if (dt.Rows[dt_row_index][ColumnsName] != System.DBNull.Value)
					{
						string value = dt.Rows[dt_row_index][ColumnsName].ToString();
						if (!string.IsNullOrEmpty(value))
						{
							result = System.Convert.ToDecimal(value);
						}
					}
				}
			}
			catch
			{
				result = 0m;
			}
			return result;
		}
		public static string GetValueString(DataTable dt, int dt_row_index, string ColumnsName)
		{
			string result = "";
			try
			{
				if (dt.Columns.Contains(ColumnsName))
				{
					if (dt.Rows[dt_row_index][ColumnsName] != System.DBNull.Value)
					{
						result = dt.Rows[dt_row_index][ColumnsName].ToString();
					}
				}
			}
			catch
			{
				result = "";
			}
			return result;
		}
	}
}
